import InteractionUtil from '../../../shell/ui/interaction.js'
import SessionUtil, { JxbHttpClient } from '../../../data/session/session.js';
import { Url } from '../../../utils/textUtils.js';
import AppSceneUtil from '../../../data/common/appScene.js';
Page({
    data: {

    },
    onLoad: function (options) {
        wx.setNavigationBarTitle({
            title: '页面跳转中...'
        })
        InteractionUtil.showLoading(300)
        SessionUtil.getInstance().then(()=>{
            AppSceneUtil.setPageScene(this, true)
            if(!options){
                InteractionUtil.showAlert('无效的小程序码，请联系您的邀请人！').then(()=>{
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                })
                return;
            }
            // 预制顾问二维码信息
            let sceneParams = {}
            if (options.scene){ // 优先从scene字段取值，将scene中的参数解析为json对象
                sceneParams = Url.parseQuery(options.scene)
            } else {
                sceneParams = options
            }
            console.log("options:", options, 'sceneParams',sceneParams);
            // 服务端获取顾问信息接口修改，前端优先级sn -> salesman_id
            let reqBody = {}
            if (sceneParams.sn) {
                reqBody.consultant_sn=sceneParams.sn
                wx.setStorageSync("consultant_sn", sceneParams.sn)
            }  else if (sceneParams.salesman_id >= 0) {
                reqBody.salesman_id=sceneParams.salesman_id
            }
            (new JxbHttpClient()).request({
                url: 'AppMaster/AppletRegister/getConsultantsInfo',
                data: reqBody,
            }).then(jxbResp => {
                console.log('AppMaster/AppletRegister/getConsultantsInfo', jxbResp)
                InteractionUtil.hideLoading(5000)
                if (jxbResp.errorCode == 302){
                    let redirectTo = jxbResp.rawResp.redirect;
                    console.log(redirectTo)
                    switch(redirectTo){
                        case 'sellingCode/bind':
                            wx.reLaunch({
                                url: '/pages/join/tips/tips'
                            })
                            break;
                        case 'consultant/auditing': //3、二维码已被绑定，审核中
                            wx.reLaunch({
                                url: '/pages/join/result/result?state=3'
                            })
                            break;
                        default:
                            wx.switchTab({
                                url: '/pages/index/index'
                            })
                    }
                } else {//正常打开 找他买酒 页面
                    wx.navigateTo({
                        url: '/pages/register/information1/index?salesmanVCardId=' + jxbResp.rawResp.info.consultant_id
                    })
                }
            }).catch(err=>{
                InteractionUtil.hideLoading()
                InteractionUtil.showAlert(err.message, '提示').then(()=>{
                    wx.switchTab({
                        url: '/pages/index/index'
                    })
                })
            })
        })
        
    }
});